package com.xd.user.controller;


import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xd.common.model.R;
import com.xd.common.base.BaseController;
import com.xd.user.api.entity.RolePermission;
import com.xd.user.api.feign.RolePermissionFeign;
import com.xd.user.service.PermissionService;
import com.xd.user.service.RolePermissionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色权限(RolePermission)表控制层
 *
 * @author hbx
 * @since 2019-12-30 17:17:26
 */
@Slf4j
@RestController
@RequestMapping("rolePermission")
@Api(description = "RolePermission")
@Transactional(rollbackFor = Exception.class)
public class RolePermissionController extends BaseController<RolePermissionService, RolePermission, String> implements RolePermissionFeign {


    @Resource
    private PermissionService permissionService;

    /**
     * @param roleId 角色id
     * @param perIds 权限id集合
     * @return com.baomidou.mybatisplus.extension.api.R<java.lang.String>
     * @author huboxin
     * @description: TODO 重置角色权限关联信息
     * @date 15:18 2020/8/17
     **/
    @ApiOperation(value = "重置角色权限关联信息", notes = "重置角色权限关联信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id"),
            @ApiImplicitParam(name = "perIds", value = "权限id集合"),
    })
    @PostMapping("resetRolePer/{roleId}")
    public R<String> resetRolePer(@PathVariable String roleId, @RequestBody List<String> perIds) {
        this.service.remove(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getRoleId, roleId));
        List<RolePermission> collect = perIds.stream()
                .map(perId -> new RolePermission().setRoleId(roleId)
                        .setPermissionId(perId))
                .collect(Collectors.toList());
        this.service.saveBatch(collect);
        return success("操作成功");
    }



    /**
     * @author huboxin
     * @description: TODO 获取菜单树,根据角色判断树节点是否勾选
     * @param roleId 角色id
     * @date 15:55 2020/8/17
     * @return com.baomidou.mybatisplus.extension.api.R<java.util.List<cn.hutool.core.lang.tree.Tree<java.lang.String>>>
     **/
    @ApiOperation(value = "获取菜单树,根据角色判断树节点是否勾选",notes = "获取菜单树,根据角色判断树节点是否勾选")
    @ApiImplicitParams({
    	@ApiImplicitParam(name="roleId",value = "角色id"),
    })
    @GetMapping("getRolePerTree/{roleId}")
    public R<List<Tree<String>>> getRolePerTree(@PathVariable String roleId) {
        List<String> collect = this.service.listObjs(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getRoleId, roleId)
                .select(RolePermission::getPermissionId))
                .stream()
                .map(StrUtil::toString)
                .collect(Collectors.toList());
        List<Tree<String>> tree = permissionService.getTree(collect,permissionService.list());
        return success(tree);
    }



    /**
     * @author huboxin
     * @description: TODO 获取角色拥有的菜单ID集合
     * @param roleId 角色id
     * @date 15:53 2020/8/17
     * @return com.baomidou.mybatisplus.extension.api.R<java.util.List < java.lang.String>>
     **/
    @ApiOperation(value = "获取角色拥有的菜单ID集合", notes = "获取角色拥有的菜单ID集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id"),
    })
    @GetMapping("listPerIdsByRoleId/{roleId}")
    public R<List<String>> listPerIdsByRoleId(@PathVariable String roleId) {
        List<String> collect = this.service.listObjs(Wrappers.<RolePermission>lambdaQuery().eq(RolePermission::getRoleId, roleId)
                .select(RolePermission::getPermissionId))
                .stream()
                .map(StrUtil::toString)
                .collect(Collectors.toList());
        return success(collect);
    }

}
